<?php

namespace openapi\controllers;

/**
 * 上传文件
 *
 * @author peng.huang
 */
class UploadController extends \openapi\controllers\base\OpenAPIController  {

    /**
     * 初始化
     */
    public function init() {
        ENVIRONMENT !== 'dev' AND die('该文件禁止访问');
    }

    /**
     * 上传文件
     */
    public function actionIndex() {
        $result = array();
        if ($this->request->isPost()) {
            if ($_FILES['file']['error'] > 0) {
                $this->formatOutput(FALSE, $result, $_FILES['file']['error']);
                return;
            }

            $path = 'E:\\upload';
            $upload = new \peng\libraries\UploadFile($_FILES['file'], 200000);
            $msg = '上传失败';
            if (TRUE === ($isSucceed = $upload->saveAs($path, $msg))) {
                $result['file'] = $path;
                $msg = '上传成功';
            }

            $this->formatOutput($isSucceed, $result, $msg);
        }
    }

}
